<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Handlers\ImageUploadHandler;

use App\Models\Serve;
use App\Models\ServeContent;
use App\Models\ServeGl;

use DB;
class ServeController extends Controller
{
	   // 检测用户是否登录，无登录跳转到登录页面
    public function __construct()
    {
       $this->middleware('auth');
    }
    //服务列表首页
    public function index(){

    	//取出服务列表
    	$serve = Serve::OrderBy('id','desc')->get();

    	return view('serve.index',compact('serve'));
    }


    //添加服务页面
    public function create(){

    	//取出所有精品推荐
        $jingpin = DB::table('serve_attach')->get();
        
        //取出所有精品附件
        $fujian = DB::table('jingpin_fujian')->get();

    	return view('serve.add',compact('jingpin','fujian'));
    }

    //创建服务
    public function store(Request $request,ImageUploadHandler $upload){

    	// dd($request);exit;
    	//数据集合
    	$data = $request->only(['title','commit','price','is_price','vip_price','path','created_at']);

    	//赋值创建时间
    	$data['created_at'] = date('Y-m-d H:i:s');

    	//判断数据是否存在nulll
    	if(!in_array(null,$data))
    	{

    		//检测列表图是否为空
    		if(!empty($request->fileimg))
    		{

    			if($request->content != null)
    			{

    				$folder = 'serve';
    				
	    			$file = $upload->save($request->fileimg,$folder);
	    			//赋值列表图路径
	    			$data['path'] = $file['path'];

	    			//数据入库
					$sid = Serve::insertGetId($data);

					if($sid)
					{

						$status = ServeContent::insert(['sid'=>$sid,'content'=>$request->content,'created_at'=>date('Y-m-d H:i:s')]);

						if($status)
						{	
							if($request->id != null)
							{

								$num = count($request->id);

								if( $num <= 3)
								{

									$sta = array();

									foreach($request->id as $iv)
									{

										$sta[] = ServeGl::insert(['sid'=>$sid,'gl_sid'=>$iv]);
										
									}

									if(in_array(1, $sta)){

										return redirect('serve');

									}else{

										return '关联商品录入失败';
									}


								}else{

									return '关联商品不能大于3个，请重新填写';
								}
								
							}

							return redirect('serve');
						}else
						{

							return '数据录入失败';
						}
					}else
					{

						return '表单录入失败';
					}

    			}
    			
    		}else
    		{

    			return '请上传列表图';
    		}

    	}else
    	{

    		return '请填写完整表单';
    	}
    	
    }

    //接受参数展示要修改服务内容
    public function edit($id){

    	//取出当前项目信息
    	$serve = Serve::LeftJoin('serve_contents','serves.id','=','serve_contents.sid')
    						->where('serves.id',$id)
    						->select('serves.*','serve_contents.content')
    						->first();
    	//取出当前项目关联商品
    	$glserve = Serve::LeftJoin('serve_gls','serves.id','=','serve_gls.sid')
    						->where('serves.id',$id)
    						->select('serve_gls.gl_sid')
    						->get();
    	//取出所有服务				
 		$allserve = Serve::get();

 		$data = array();

 		foreach($glserve as $av){
 			$data[] = $av->gl_sid;
 		}

    	return view('serve.edit',compact('serve','data','allserve'));
    }


    //商品数据更新
    public function update(Request $request,$id,ImageUploadHandler $upload){

		//检测列表图是否为空
		if(!empty($request->fileimg))
		{
			//数据集合
			$data = $request->only(['title','commit','price','is_price','vip_price','path']);

			$folder = 'serve';
			$file = $upload->save($request->fileimg,$folder);
			//赋值列表图路径
			$data['path'] = $file['path'];

		}else
		{
			//数据集合
			$data = $request->only(['title','commit','price','is_price','vip_price']);
		}

    	//判断数据是否存在nulll
    	if(!in_array(null,$data))
    	{
			if($request->content != null)
			{
    			//数据入库
				$sid = Serve::where('id',$id)->update($data);

				if($sid)
				{
					$status = ServeContent::where('sid',$id)->update(['content'=>$request->content]);

					if($status)
					{

						ServeGl::where('sid',$id)->delete();
							
							if($request->id != null)
							{

								$num = count($request->id);

								if( $num <= 3)
								{

									$sta = array();

									foreach($request->id as $iv)
									{

										$sta[] = ServeGl::insert(['sid'=>$id,'gl_sid'=>$iv]);
										
									}

									if(in_array(1, $sta)){

										return redirect('serve');

									}else{

										return '关联商品录入失败';
									}


								}else{

									return '关联商品不能大于3个，请重新填写';
								}
								
							}

						return redirect('serve');

					}else
					{

						return '数据录入失败';
					}
				}else
				{

					return '表单录入失败';
				}

    		}
    			
    		

    	}else
    	{

    		return '请填写完整表单';
    	}
    }

    //删除服务
    public function destroy(Request $request){

    	if($request->id == null){

    		return '请选择后再点击删除';
    	}

    	$data = array();
    	foreach($request->id as $sv){

			$status = ServeContent::where('sid',$sv)->delete();

			if($status){

				$data[] = Serve::where('id',$sv)->delete();
				
				ServeGl::where('sid',$sv)->delete();
			}

    	}

    	if(in_array(1, $data)){

    		return back();
    	}else{

    		return '删除出错';
    	}
    }	

}
